package com.cmhi.base.provider;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.text.TextUtils;

import com.softmbh.kellyplatform.util.AppUtils;

public class AppInfoManager extends BimDataManager{
    private Context mContext;
    private ContentResolver mContentResolver;
    private ContentValues mContentValues;
    private String selection;
    private Uri mUri;

    public AppInfoManager(Context context) {
        super();
        mContext = context;
        mContentResolver = mContext.getApplicationContext().getContentResolver();
        mContentValues = new ContentValues();
        selection = "name=?";
        mUri = BimProvider.HUA_WEI_CONTENT_URI;
    }
    @Override
    public String getValue(String key) {
        String cacheValue = BimDataManager.getAppInfoMap().get(key);
        if(!TextUtils.isEmpty(cacheValue)){
            return cacheValue;
        }
        Cursor cursor = mContentResolver.query(
                mUri, null, selection, new String[]{key}, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                String value = cursor.getString(cursor.getColumnIndex(BimDBHelper.HUA_WEI_COLUMNS_VALUE));
                cursor.close();
                AppUtils.debugLog("query: caller=" + mContext.getPackageName()+","
                        +"table="+BimDBHelper.HUA_WEI_TABLE_NAME+","
                        +BimDBHelper.HUA_WEI_COLUMNS_NAME+"="+key+","
                        +BimDBHelper.HUA_WEI_COLUMNS_VALUE+"="+value);
                return ((value == null) ? "" : value);
            }
        }
        return "";
    }

    @Override
    public void update(String key, String value) {
        BimDataManager.getAppInfoMap().put(key,value);
        mContentValues.clear();
        mContentValues.put(BimDBHelper.HUA_WEI_COLUMNS_NAME, key);
        mContentValues.put(BimDBHelper.HUA_WEI_COLUMNS_VALUE, value);
        Cursor cursor = mContentResolver.query(
                mUri, null, selection, new String[]{key}, null);
        if (cursor.getCount() > 0) {
            mContentResolver.update(mUri, mContentValues, selection, null);
            AppUtils.debugLog("update: caller=" + mContext.getPackageName()+","
                    +"table="+BimDBHelper.HUA_WEI_TABLE_NAME+","
                    +BimDBHelper.HUA_WEI_COLUMNS_NAME+"="+key+","
                    +BimDBHelper.HUA_WEI_COLUMNS_VALUE+"="+value);
        } else {
            mContentResolver.insert(mUri, mContentValues);
            AppUtils.debugLog("insert: caller=" + mContext.getPackageName()+","
                    +"table="+BimDBHelper.HUA_WEI_TABLE_NAME+","
                    +BimDBHelper.HUA_WEI_COLUMNS_NAME+"="+key+","
                    +BimDBHelper.HUA_WEI_COLUMNS_VALUE+"="+value);
        }
        cursor.close();
    }

    @Override
    public void delete(String key) {
        BimDataManager.getAppInfoMap().remove(key);
        Cursor cursor = mContentResolver.query(
                mUri, null, selection, new String[]{key}, null);
        if (cursor.getCount() > 0) {
            mContentResolver.delete(mUri, selection, new String[]{key});
            AppUtils.debugLog("delete: caller=" + mContext.getPackageName()+","
                    +"table="+BimDBHelper.HUA_WEI_TABLE_NAME+","
                    +BimDBHelper.HUA_WEI_COLUMNS_NAME+"="+key);
        }
        cursor.close();
    }
}
